package com.codejob.portal.web.action.project;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.codejob.portal.dao.utils.Page;
import com.codejob.portal.dao.utils.PropertyFilter;
import com.codejob.portal.entity.project.Address;
import com.codejob.portal.entity.project.ProjectType;
import com.codejob.portal.entity.project.Projects;
import com.codejob.portal.services.impl.project.AddressManager;
import com.codejob.portal.services.impl.project.ProjectManager;
import com.codejob.portal.services.impl.project.ProjectTypeManager;
import com.codejob.portal.services.intf.BusinessExcption;
import com.codejob.portal.web.BaseModelAction;
import com.codejob.portal.web.utils.Struts2Utils;

@Namespace("/project")
public class ProjectAction extends BaseModelAction<Projects> {

	private static final long serialVersionUID = -2067631919100973633L;

	@Autowired
	private ProjectManager projManager;
	@Autowired
	private AddressManager addManager;
	@Autowired
	private ProjectTypeManager typeManager;

	private Long id;

	private Projects project;
	
	private String typeFilter;

	private Page<Projects> page=new Page<Projects>(5);

	private List<Address> addressList;

	private List<ProjectType> typeList;
	
	private List<ProjectType> typeGroupList;

	public String projectIndex() {
		try {
			typeGroupList = typeManager.getTypeGroupList();
			List<PropertyFilter> filters = Struts2Utils.buildFromHttpRequest();
			//设置默认排序方式
			if (page!=null&&!page.isOrderBySetted()) {
				page.setOrderBy("id");
				page.setOrder(Page.DESC);
			}
			page = projManager.getProjectPage(page,filters);
		} catch (BusinessExcption e) {
			e.printStackTrace();
		}
		return "projectIndex";
	}
	
	public String projectFilter() {
		try {
			typeGroupList = typeManager.getTypeGroupList();
			//设置默认排序方式
			if (page!=null&&!page.isOrderBySetted()) {
				page.setOrderBy("id");
				page.setOrder(Page.DESC);
			}
			//System.out.println(typeFilter);
			if(typeFilter!=null&&!typeFilter.equals("")){
				page = projManager.getProjectFilterPage(page,typeFilter);
			}else{
				return this.projectIndex();
			}
			
		} catch (BusinessExcption e) {
			e.printStackTrace();
		}
		return "projectFilter";
	}

	public String prepareCreate() {
		try {
			if (isLogin()) {
				addressList = addManager.getList();
				typeList = typeManager.getList();
			} else {
				return "login";
			}
		} catch (BusinessExcption e) {
			e.printStackTrace();
		}
		return "create";
	}

	public String create() {
		try {
			if (isLogin()) {
				projManager.create(project, getUserId());
			} else {
				return "login";
			}
		} catch (BusinessExcption e) {
			e.printStackTrace();
		}
		return "createSuccess";
	}


	public String look(){
		project=projManager.look(id);
		return "look";
	}


	public Page<Projects> getPage() {
		return page;
	}

	public void setPage(Page<Projects> page) {
		this.page = page;
	}

	public List<Address> getAddressList() {
		return addressList;
	}

	public void setAddressList(List<Address> addressList) {
		this.addressList = addressList;
	}

	public List<ProjectType> getTypeList() {
		return typeList;
	}

	public void setTypeList(List<ProjectType> typeList) {
		this.typeList = typeList;
	}

	public Projects getProject() {
		return project;
	}

	public void setProject(Projects project) {
		this.project = project;
	}

	@Override
	public void prepare() throws Exception {
		if (id != null) {
			project = projManager.get(id);
		}
		if (project == null) {
			project = new Projects();
		}
	}

	@Override
	public Projects getModel() {
		return project;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<ProjectType> getTypeGroupList() {
		return typeGroupList;
	}

	public void setTypeGroupList(List<ProjectType> typeGroupList) {
		this.typeGroupList = typeGroupList;
	}

	public String getTypeFilter() {
		return typeFilter;
	}

	public void setTypeFilter(String typeFilter) {
		this.typeFilter = typeFilter;
	}

}
